package com.example.rbac.uitls;

import java.util.concurrent.*;

public class ThreadUtils {

    public static void Run(Runnable runnable, int num) {
        synchronized (runnable){
            for (int i = 0; i < num; i++) {
                Thread thread = new Thread(runnable);
                thread.start();
            }
        }
    }

    public static void Run(Runnable runnable, int num, long time)throws Exception {
        synchronized (runnable){
            for (int i = 0; i < num; i++) {
                TimeUnit.SECONDS.sleep(time);
                Thread thread = new Thread(runnable);
                thread.start();
            }
        }
    }

    public static  void threadPool(int corePoolSize,
                           int maximumPoolSize,
                           long keepAliveTime,
                           TimeUnit unit,
                           LinkedBlockingQueue<Runnable> runnables){
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, queue);
        for (Runnable runnable : runnables){
            threadPoolExecutor.execute(runnable);
        }
    }
}
